//
//  LoginView.m
//  FruitVIP
//
//  Created by 孙艳玲 on 15/5/22.
//  Copyright (c) 2015年 孙艳玲. All rights reserved.
//

#import "LoginView.h"
#import "AppDelegate.h"

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)


@implementation LoginView
- (instancetype)initWithFrame:(CGRect)frame
{
    self =[ super initWithFrame:frame];
    if (self) {
        [self createSub];
    }
    return self;
}

- (void)createSub
{
     self.isSelected = NO;
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT/4)];
    UIImage *image = [UIImage imageNamed:@"firstPage.png"];
    imageView.image = image;
//    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:imageView];
    
    

    
    UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, imageView.frame.origin.y + imageView.frame.size.height + 20, SCREEN_WIDTH/6, 25)];
    phoneLabel.backgroundColor = [UIColor whiteColor];
    phoneLabel.text = @"手机号";
    [self addSubview:phoneLabel];
    
    self.nameText = [[UITextField alloc] initWithFrame:CGRectMake(phoneLabel.frame.origin.x + phoneLabel.frame.size.width, phoneLabel.frame.origin.y, SCREEN_WIDTH - 20 - phoneLabel.frame.size.width, phoneLabel.frame.size.height)];
    self.nameText.backgroundColor = [UIColor whiteColor];
    self.nameText.clearsOnInsertion = YES;
    self.nameText.clearButtonMode = UITextFieldViewModeAlways;
    [self addSubview:self.nameText];
    
    UILabel *linePhone = [[UILabel alloc] initWithFrame:CGRectMake(phoneLabel.frame.origin.x, phoneLabel.frame.origin.y + phoneLabel.frame.size.height + 3, SCREEN_WIDTH - 20, 1)];
    linePhone.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1];
    [self addSubview:linePhone];
    
    UILabel *passLabel = [[UILabel alloc] initWithFrame:CGRectMake(phoneLabel.frame.origin.x, linePhone.frame.origin.y + linePhone.frame.size.height + 10, phoneLabel.frame.size.width, phoneLabel.frame.size.height)];
    passLabel.backgroundColor = [UIColor whiteColor];
    passLabel.text = @"密码";
    [self addSubview:passLabel];
    
    self.passText = [[UITextField alloc] initWithFrame:CGRectMake(self.nameText.frame.origin.x, passLabel.frame.origin.y, self.nameText.frame.size.width, passLabel.frame.size.height)];
    self.passText.secureTextEntry = YES;//设置密码密文输入
    self.passText.clearsOnInsertion = YES;
    self.passText.clearButtonMode = UITextFieldViewModeAlways;
    [self addSubview:self.passText];
    
    UILabel *passLine = [[UILabel alloc] initWithFrame:CGRectMake(passLabel.frame.origin.x, passLabel.frame.origin.y +passLabel.frame.size.height + 3, SCREEN_WIDTH - 20, 1)];
    passLine.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1];
    [self addSubview:passLine];
 
    
    
    self.selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.selectButton.frame = CGRectMake(passLabel.frame.origin.x, passLabel.frame.origin.y + passLabel.frame.size.height + 10, 20, 20);

    //边框颜色
    [self.selectButton.layer setBorderWidth:1.0];
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 230/255.0, 230/255.0, 230/255.0, 1});
    [self.selectButton.layer setBorderColor:colorref];
        [self.selectButton addTarget:self action:@selector(selectButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.selectButton];
    
    UILabel *remLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.selectButton.frame.origin.x + self.selectButton.frame.size.width + 5, self.selectButton.frame.origin.y, (SCREEN_WIDTH-20) / 3, self.selectButton.frame.size.height)];
        remLabel.text = @"记住密码";
        remLabel.font = [UIFont systemFontOfSize:15];
    remLabel.textColor = [UIColor blackColor];
//        remLabel.textColor = [UIColor colorWithRed:111.0/255.0 green:79.0/255.0 blue:71.0/255.0 alpha:1];
        [self addSubview:remLabel];
    
        UIButton *passWordButton = [UIButton buttonWithType:UIButtonTypeCustom];
        passWordButton.frame = CGRectMake( SCREEN_WIDTH - 20 - (SCREEN_WIDTH - 20) /4, remLabel.frame.origin.y, (SCREEN_WIDTH - 20) /4, remLabel.frame.size.height);
        [passWordButton setTitle:@"忘记密码？" forState:UIControlStateNormal];
        [passWordButton setTitleColor:[UIColor colorWithRed:111.0/255.0 green:79.0/255.0 blue:71.0/255.0 alpha:1] forState:UIControlStateNormal];
        passWordButton.titleLabel.font = [UIFont systemFontOfSize:15];
        passWordButton.titleLabel.textAlignment = NSTextAlignmentRight;
    //下划线
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc] initWithString:@"忘记密码？"];
    NSRange contentRange = {0,[content length]};
    [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
    passWordButton.titleLabel.attributedText = content;

    [passWordButton addTarget:self action:@selector(passWordButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:passWordButton];

        UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        loginButton.frame = CGRectMake( remLabel.frame.size.width/2 , passWordButton.frame.origin.y + passWordButton.frame.size.height + 40, SCREEN_WIDTH - 20  - remLabel.frame.size.width/2 - passWordButton.frame.size.width/2, SCREEN_HEIGHT / 15);
        loginButton.backgroundColor = [UIColor colorWithRed:242.0 / 255.0 green:204.0 / 255.0 blue:49.0 / 255.0 alpha:1];
        [loginButton setTitle:@"登陆" forState:UIControlStateNormal];
    [loginButton setTitleColor:[UIColor colorWithRed:110.0 / 255.0 green:78.0 / 255.0 blue:71.0 / 255.0 alpha:1] forState:UIControlStateNormal];
      loginButton.layer.cornerRadius = CGRectGetHeight([loginButton bounds])/2;
        [loginButton addTarget:self action:@selector(loginButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:loginButton];

        UIButton *registButton = [UIButton buttonWithType:UIButtonTypeCustom];
        registButton.frame = CGRectMake(loginButton.frame.origin.x, loginButton.frame.origin.y + loginButton.frame.size.height + 10, loginButton.frame.size.width, loginButton.frame.size.height);
        [registButton setTitle:@"注册" forState:UIControlStateNormal];
    [registButton setTitleColor:[UIColor colorWithRed:242.0 / 255.0 green:204.0 / 255.0 blue:49.0 / 255.0 alpha:1] forState:UIControlStateNormal];
     registButton.layer.cornerRadius = CGRectGetHeight([registButton bounds])/2;
        registButton.backgroundColor = [UIColor colorWithRed:110.0 / 255.0 green:78.0 / 255.0 blue:71.0 / 255.0 alpha:1];
        [registButton addTarget:self action:@selector(registButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:registButton];

    
    
    self.nameText.delegate = self;
    self.passText.delegate = self;
    
}

//点击键盘return键触发的方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //点击return取消第一响应者
    [self.nameText resignFirstResponder];
    [self.passText resignFirstResponder];
    return YES;
}

- (void)passWordButtonAction
{
    NSLog(@"忘记密码");
    [self.forgetDelegate forgetButton];
}

- (void)loginButton
{
    NSLog(@"登陆");
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    if ([self.nameText.text isEqualToString:@""] || [self.passText.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"用户名或密码不能为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alert.tag = 1001;
        [alert show];
    }
    else
    {
        if ([self.nameText.text isEqualToString:[userDefault objectForKey:self.passText.text]]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"登陆成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            alert.tag = 1003;
            [alert show];
         

            
            
        }
        else
        {
            if (![self.nameText.text isEqualToString:@"11"] || ![self.passText.text isEqualToString:@"11"]) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"用户名或密码输入错误" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                alert.tag = 1002;
                [alert show];
            }
            

        }
        
    }
}

- (void)registButton
{
    NSLog(@"注册");
    [self.myDelegate myButton];
    
}

// 点击alertView的方法 (签协议)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{
    if (alertView.tag == 1003) {
        [self.loginDelegate loginButton];
        
        

    }
   }
- (void)selectButtonAction:(UIButton *)button
{
    NSLog(@"勾选");
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    if (self.isSelected == NO) {
        self.isSelected = YES;
        [button setImage:[UIImage imageNamed:@"regist.png"] forState:UIControlStateNormal];
        self.nameText.text = [userDefault objectForKey:self.passText.text];
           }
    else{
        self.isSelected = NO;
        [button setImage:[UIImage imageNamed:nil] forState:UIControlStateNormal];
    }

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
